package org.iteam.xlz.core.service.user;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import org.iteam.db.BaseService;
import org.iteam.xlz.core.entity.user.UserRelation;

public class UserRelationService extends BaseService<UserRelation>{
	private static UserRelationService instance;
	private UserRelationService(){}
	
	public static UserRelationService getInstance(){
		if(null==instance){
			instance=new UserRelationService();
		}
		return instance;
	}
	
	public long counts(String superioruid){
		try {
			return super.counts("select count(1) from userrelation where superioruid=?",superioruid);
		} catch (SQLException e) {
			e.printStackTrace();
			return 0;
		}
	}
	
	public List<UserRelation> query(String superioruid,long page,long rows){
		try {
			return super.query("select r.username as subordinateuid,r.mobile as superioruid,r.status as royaltyrate,l.createtime"+
		" from userrelation l left join userinfo r on l.subordinateuid=r.uid where l.superioruid=? limit ?,?",superioruid,(page-1)*rows,rows);
		} catch (SQLException e) {
			e.printStackTrace();
			return new ArrayList<UserRelation>();
		}
	}
	
	public UserRelation getOneBySubordinateuid(String subordinateuid){
		try {
			return super.show("select * from userrelation where subordinateuid=?",subordinateuid);
		} catch (SQLException e) {
			e.printStackTrace();
			return null;
		}
	}
}
